import sys
from run import app

from app.db.models.users import User
from app.db.models.asset_tasks import AssetTasks
from app.libs.utils import enable_task_group


def fix_asset_change_task(user_name):
    user = User.objects().find_one(
        {
            'username': user_name
        }
    )
    if not user:
        print("用户不存在")
        return
    asset_tasks = AssetTasks.objects.filter(uid=user.id, taskGroupType="web_asset", triggerType="schedule")
    if not asset_tasks:
        print("资产变更任务不存在")
        return
    for asset_task in asset_tasks:
        if not asset_task.enableMonitor:
            asset_task.update(enableMonitor=True)
        else:
            print("资产变更任务已处于开启状态，无需重复开启")
            continue
        enable_task_group(asset_task, insert=True)


if __name__ == '__main__':
    with app.app_context():
        username = sys.argv[1]
        fix_asset_change_task(username)
